package com.cloudbroker.bcs.common.service;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.cloudbroker.bcs.common.constants.BasicErrorCodes;
import com.cloudbroker.bcs.common.constants.BasicFields;
import com.cloudbroker.bcs.common.context.RuntimeContext;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.message.ExtendMessageSource;
import com.cloudbroker.bcs.common.util.ValueUtil;

public class ErrorDescService {
    
    public static final String MSG_PREFIX_ERROR_INFO = "errorInfo.";
    
    public static final String MSG_PREFIX_ERROR_CONVERT = "errorConvert.";
    
    public static final String MSG_PREFIX_FIELD_CONVERT = "fieldConvert.";
    
    public static final String MSG_KEY_DEFAULT_SUCCESS = MSG_PREFIX_ERROR_INFO + BasicErrorCodes.SUCCESS;
    
    public static final String MSG_KEY_DEFAULT_ERROR = MSG_PREFIX_ERROR_INFO + BasicErrorCodes.COMMON_ERROR;
    
    public static final String MSG_KEY_DEFAULT_PARAM_ERROR = MSG_PREFIX_ERROR_INFO + BasicErrorCodes.PARAM_ERROR;
    
    private static ErrorDescService errorDescService;
    
    private String msgPrefixErrorInfo = MSG_PREFIX_ERROR_INFO;
    
    private String msgPrefixErrorConvert = MSG_PREFIX_ERROR_CONVERT;
    
    private String msgKeyDefaultSuccess;
    
    private String msgKeyDefaultError;
    
    private String msgKeyDefaultParamError;
    
    private String errorCodeSuccess = BasicErrorCodes.SUCCESS;
    
    private String errorCodeError = BasicErrorCodes.COMMON_ERROR;
    
    private String errorCodeParamError = BasicErrorCodes.PARAM_ERROR;
    
    private ExtendMessageSource messageSource;
    
    public static ErrorDescService getInstance() {
        if (null == errorDescService) {
            errorDescService = new ErrorDescService();
        }
        return errorDescService;
    }
    
    public void init() {
        msgKeyDefaultSuccess = msgPrefixErrorInfo + errorCodeSuccess;
        msgKeyDefaultError = msgPrefixErrorInfo + errorCodeError;
        msgKeyDefaultParamError = msgPrefixErrorInfo + errorCodeParamError;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void descriptError(Object result) {
        if (result instanceof Map) {
            Map map = (Map) result;
            setErrorDescInSimpleMap(map, null);
            /*try {
                List<Map> errors = (List<Map>) map.get(BasicFields.ERRORS);
                for (Map error : errors) {
                    setErrorDescInSimpleMap(error, getDefaultErrorDesc());
                }
            } catch (Exception e) {
                // omit
            }*/
        } else if( result instanceof HSBCPSBackVO ) {
            HSBCPSBackVO form = (HSBCPSBackVO) result;
            setErrorDesc(form, null);
        }
    }

    private void setErrorDesc(HSBCPSBackVO form, String defaultMessage) {
        //修改错误返回增加后台出错信息
        String errorInfo = form.getError_info();
        String errorNo = ValueUtil.getString(form.getError_no());
        if (StringUtils.isNotBlank(errorNo)) {
            // 转换错误码
            errorNo = convertErrorCode(errorNo);
            // 如果errorOn非空，结合errorOn查找
            String errorOn = ValueUtil.getString(form.getError_on());
            if (StringUtils.isNotBlank(errorOn)) {
                errorInfo = getErrorDesc(errorNo + '.' + errorOn, (String) null);
            }
            // 如果查不到，按errorNo查找
            if (StringUtils.isBlank(errorInfo)) {
                errorInfo = getErrorDesc(errorNo, (String) null);
            }
            // 如果查不到，使用默认错误描述
            if (StringUtils.isBlank(errorInfo)) {
                if (null == defaultMessage) {
                    errorInfo = getDefaultDesc(errorNo);
                } else {
                    errorInfo = defaultMessage;
                }
            }
        } else {
            if (null == defaultMessage) {
                errorInfo = getDefaultDesc(null);
            } else {
                errorInfo = defaultMessage;
            }
        }
        // 设置错误描述
        form.setError_info(errorInfo);
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void setErrorDescInSimpleMap(Map map, String defaultMessage) {
        String errorInfo = null;
        String errorNo = ValueUtil.getString(map.get(BasicFields.ERROR_NO));
        if (StringUtils.isNotBlank(errorNo)) {
            // 转换错误码
            errorNo = convertErrorCode(errorNo);
            // 如果errorOn非空，结合errorOn查找
            String errorOn = ValueUtil.getString(map.get(BasicFields.ERROR_ON));
            if (StringUtils.isNotBlank(errorOn)) {
                errorInfo = getErrorDesc(errorNo + '.' + errorOn, (String) null);
            }
            // 如果查不到，按errorNo查找
            if (StringUtils.isBlank(errorInfo)) {
                errorInfo = getErrorDesc(errorNo, (String) null);
            }
            // 如果查不到，使用默认错误描述
            if (StringUtils.isBlank(errorInfo)) {
                if (null == defaultMessage) {
                    errorInfo = getDefaultDesc(errorNo);
                } else {
                    errorInfo = defaultMessage;
                }
            }
        } else {
            if (null == defaultMessage) {
                errorInfo = getDefaultDesc(null);
            } else {
                errorInfo = defaultMessage;
            }
        }
        // 设置错误描述
        map.put(BasicFields.ERROR_INFO, errorInfo);
    }
    
    private String convertErrorCode(String errorCode) {
        return getMessageSource().getMessage(msgPrefixErrorConvert + errorCode, errorCode);
    }
    
    public String getErrorDesc(String errorCode) {
        return getMessageSource().getMessage(msgPrefixErrorInfo + errorCode);
    }
    
    public String getErrorDesc(String errorCode, String defaultMessage) {
        return getMessageSource().getMessage(msgPrefixErrorInfo + errorCode, defaultMessage);
    }
    
    public String getFieldConvert(String fieldName) {
        return getMessageSource().getMessage(MSG_PREFIX_FIELD_CONVERT + fieldName);
    }
    
    public String getFieldConvert(String fieldName, String defaultValue) {
        return getMessageSource().getMessage(MSG_PREFIX_FIELD_CONVERT + fieldName, defaultValue);
    }
    
    public String getErrorDesc(String errorCode, Object[] args) {
        return getMessageSource().getMessage(msgPrefixErrorInfo + errorCode, args);
    }
    
    public String getErrorDesc(String errorCode, Object[] args, String defaultMessage) {
        return getMessageSource().getMessage(msgPrefixErrorInfo + errorCode, args, defaultMessage);
    }
    
    public String getDefaultDesc(String errorCode) {
        if (StringUtils.isBlank(errorCode) || errorCodeSuccess.equals(errorCode)) {
            return getDefaultSuccessDesc();
        } else {
            return getDefaultErrorDesc();
        }
    }
    
    public String getDefaultSuccessDesc() {
        return getMessageSource().getMessage(msgKeyDefaultSuccess, "成功");
    }
    
    public String getDefaultErrorDesc() {
        return getMessageSource().getMessage(msgKeyDefaultError, "系统繁忙");
    }
    
    public String getDefaultParamErrorDesc() {
        return getMessageSource().getMessage(msgKeyDefaultParamError, getDefaultErrorDesc());
    }
    
    public String getMsgPrefixErrorInfo() {
        return msgPrefixErrorInfo;
    }
    
    public void setMsgPrefixErrorInfo(String msgPrefixErrorInfo) {
        if (StringUtils.isNotBlank(msgPrefixErrorInfo)) {
            this.msgPrefixErrorInfo = msgPrefixErrorInfo;
        }
    }
    
    public String getMsgPrefixErrorConvert() {
        return msgPrefixErrorConvert;
    }
    
    public void setMsgPrefixErrorConvert(String msgPrefixErrorConvert) {
        if (StringUtils.isNotBlank(msgPrefixErrorConvert)) {
            this.msgPrefixErrorConvert = msgPrefixErrorConvert;
        }
    }
    
    public String getErrorCodeSuccess() {
        return errorCodeSuccess;
    }
    
    public void setErrorCodeSuccess(String errorCodeSuccess) {
        if (StringUtils.isNotBlank(errorCodeSuccess)) {
            this.errorCodeSuccess = errorCodeSuccess;
        }
    }
    
    public String getErrorCodeError() {
        return errorCodeError;
    }
    
    public void setErrorCodeError(String errorCodeError) {
        if (StringUtils.isNotBlank(errorCodeError)) {
            this.errorCodeError = errorCodeError;
        }
    }
    
    public String getErrorCodeParamError() {
        return errorCodeParamError;
    }
    
    public void setErrorCodeParamError(String errorCodeParamError) {
        if (StringUtils.isNotBlank(errorCodeParamError)) {
            this.errorCodeParamError = errorCodeParamError;
        }
    }
    
    public ExtendMessageSource getMessageSource() {
        if (null == messageSource) {
            messageSource = RuntimeContext.getBean(ExtendMessageSource.class);
        }
        return messageSource;
    }
    
    public void setMessageSource(ExtendMessageSource messageSource) {
        this.messageSource = messageSource;
    }
    
}
